<template>
  <view class="share-list-container">
     <!-- 顶部标题栏 -->
    <view class="header">
      <view class="back-btn" @click="goBack">
        <text class="back-arrow">←</text>
      </view>
      <text class="title">我的分享列表</text>
    </view>
    
    <!-- 搜索框 -->
    <!-- <view class="search-section">
      <view class="search-input">
        <image src="/static/images/search.png" mode="aspectFit" class="search-icon"></image>
        <input type="text" placeholder="搜索分享用户" v-model="searchKeyword"/>
      </view>
    </view> -->
    
    <!-- 筛选标签 -->
    <!-- <view class="filter-tags">
      <view class="filter-tag active" @click="setFilter('all')">全部</view>
      <view class="filter-tag" @click="setFilter('registered')">已注册</view>
    </view> -->
    
    <!-- 分享列表 -->
    <view class="share-list">
      <!-- 分享项 -->
      <view class="share-item" v-for="(item, index) in shareRecords" :key="index">
        <view class="share-header">
          <view class="user-info" v-if="item.memberSumma">
            <view class="avatar">
              <image :src="item.memberSumma.avatar" mode="aspectFit"></image>
            </view>
            <view class="user-details">
              <text class="username">{{ item.memberSumma.username }}</text>
              <text class="user-id">ID: {{ item.memberSumma?.id }}</text>
            </view>
          </view>
          <text class="share-time">{{ item.createdAt }}</text>
        </view>
        
        <!-- <view class="share-status">
          <text class="status-text">{{ item.status }}</text>
          <text class="status-time">{{ item.statusTime }}</text>
        </view> -->
      </view>
      
      <!-- 空状态 -->
      <view class="empty-state" v-if="shareRecords.length === 0">
        <image src="/static/images/empty.png" mode="aspectFit" class="empty-image"></image>
        <text class="empty-text">暂无分享记录</text>
      </view>
    </view>
    
    
  </view>
</template>

<script>
import { get } from '../../utils/request.js'
export default {
  data() {
    return {
      shareRecords: [],
      searchKeyword: '',
      currentFilter: 'all',
      loading: false,
      page: 1,
      pageSize: 10,
      hasMore: true
    }
  },
  
  onLoad() {
    // 页面加载时获取分享列表
    this.getShareList()
  },
  
  methods: {
    // 返回上一页
    goBack() {
      uni.navigateBack()
    },
    // 获取分享列表
    async getShareList() {
      if (this.loading || !this.hasMore) {
        return
      }
      
      this.loading = true
      try {
        const token = uni.getStorageSync('token')
        if (!token) {
          uni.showToast({
            title: '请先登录',
            icon: 'none'
          })
          return
        }
        
        // 调用API获取分享列表
        const res = await get('/api/dify/member/inviteList', {
          page: this.page,
          pageSize: this.pageSize,
          keyword: this.searchKeyword,
          filter: this.currentFilter
        })
        
        console.log('API返回结果:', res)
        
        if (res.code === 0 && res.data) {
          // 检查数据格式并进行适配
          const dataList = res.data.list || []
          const newRecords = dataList
          
          if (newRecords.length < this.pageSize) {
            this.hasMore = false
          }
          
          // 如果是第一页，直接替换数据；否则追加
          if (this.page === 1) {
            this.shareRecords = newRecords.length > 0 ? newRecords : this.shareRecords
          } else {
            this.shareRecords = [...this.shareRecords, ...newRecords]
          }
          
          this.page++
        } else {
          this.hasMore = false
          // 如果是第一页且没有数据，保持模拟数据
          if (this.page === 1 && this.shareRecords.length === 0) {
            // 保持原有的模拟数据
          }
        }
        
      } catch (error) {
        console.warn('API调用失败，使用模拟数据:', error)
        // API调用失败时，保持原有数据不变
        if (this.page === 1) {
          // 第一页时保持模拟数据
        } else {
          // 加载更多时失败，提示用户
          uni.showToast({
            title: '加载更多失败',
            icon: 'none'
          })
        }
      } finally {
        this.loading = false
      }
    },
    
    // 设置筛选条件
    setFilter(filter) {
      this.currentFilter = filter
      // 重置列表
      this.page = 1
      this.hasMore = true
      // 重新获取数据
      this.getShareList()
    },
    
    // 搜索
    handleSearch() {
      // 重置列表
      this.page = 1
      this.hasMore = true
      // 重新获取数据
      this.getShareList()
    },
    
    // 上拉加载更多
    onReachBottom() {
      this.getShareList()
    }
  }
}
</script>

<style scoped>
.share-list-container {
  background-color: #3A3052;
  min-height: 100vh;
  padding-bottom: 120rpx;
}


.header {
  background-color: #4A4062;
  padding: 10px 65px 10px 15px;
  display: flex;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 100;
}

.back-btn {
  width: 40px;
  height: 40px;
  background-color: rgba(240, 217, 181, 0.1);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.back-btn:active {
  background-color: rgba(240, 217, 181, 0.2);
  transform: scale(0.95);
}

.back-arrow {
  color: #F0D9B5;
  font-size: 20px;
  font-weight: bold;
}

.title {
  flex: 1;
  color: rgba(255, 255, 255, 0.5);
  font-size: 16px;
  font-weight: 600;
  text-align: center;
}

.search-section {
  padding: 20rpx;
}

.search-input {
  background-color: #4A4062;
  border-radius: 8rpx;
  padding: 20rpx;
  display: flex;
  align-items: center;
}

.search-icon {
  width: 32rpx;
  height: 32rpx;
  margin-right: 15rpx;
  opacity: 0.6;
}

.search-input input {
  color: #FFFFFF;
  font-size: 32rpx;
  background-color: transparent;
  border: none;
  flex: 1;
}

.search-input input::placeholder {
  color: #8F82A8;
  font-size: 32rpx;
}

.filter-tags {
  display: flex;
  padding: 0 20rpx 20rpx;
  gap: 20rpx;
}

.filter-tag {
  flex: 1;
  background-color: #4A4062;
  color: #8F82A8;
  font-size: 32rpx;
  text-align: center;
  padding: 20rpx;
  border-radius: 8rpx;
}

.filter-tag.active {
  background-color: #F8E7D0;
  color: #3A3052;
}

.share-list {
  padding: 0 20rpx 20rpx;
}

.share-item {
  background-color: #4A4062;
  border-radius: 16rpx;
  padding: 25rpx;
  margin: 20rpx 0;
}

.share-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.user-info {
  display: flex;
  align-items: center;
}

.avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  background-color: #F8E7D0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20rpx;
  overflow: hidden;
}

.avatar image {
  width: 100%;
  height: 100%;
}

.user-details {
  display: flex;
  flex-direction: column;
}

.username {
  color: #FFFFFF;
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 5rpx;
}

.user-id {
  color: #8F82A8;
  font-size: 28rpx;
}

.share-time {
  color: #8F82A8;
  font-size: 28rpx;
}

.share-status {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.status-text {
  color: #F0D9B5;
  font-size: 32rpx;
  font-weight: bold;
}

.status-time {
  color: #8F82A8;
  font-size: 28rpx;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
}

.empty-image {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 30rpx;
  opacity: 0.5;
}

.empty-text {
  color: #8F82A8;
  font-size: 32rpx;
}

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 120rpx;
  background-color: #2A2240;
  display: flex;
  justify-content: space-around;
  align-items: center;
  border-top: 1rpx solid #3A3052;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #8F82A8;
}

.nav-item.active {
  color: #F0D9B5;
}

.nav-item image {
  width: 50rpx;
  height: 50rpx;
  margin-bottom: 10rpx;
}

.nav-item text {
  font-size: 24rpx;
}
</style>